import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/global/global_api.dart';
import 'package:flutter_app/main/content/content.dart';
import 'package:flutter_app/main/content/model/content_model.dart';
import 'package:flutter_app/main/main/model/system_nav_entity.dart';

class SystemNavWidget extends StatefulWidget {
  const SystemNavWidget({super.key});

  @override
  SystemNavPage createState() => SystemNavPage();
}

class SystemNavPage extends State<SystemNavWidget>
    with AutomaticKeepAliveClientMixin {
  List<SystemNavData> dataList = [];
  List<List<Widget>> itemWidgetList = [];

  Future<void> getSystemList() async {
    String url = GlobalApi.systemNavUrl;
    Response response = await Dio().get(url);
    var data = response.data;
    SystemNavEntity systemEntity = SystemNavEntity.fromJson(data);
    setState(() {
      dataList.clear();
      dataList.addAll(systemEntity.data);
      for (int i = 0; i < dataList.length; i++) {
        List<Widget> widgets = [];
        for (int j = 0; j < dataList[i].articles.length; j++) {
          widgets.add(addFlowWidget(dataList[i].articles[j]));
        }
        itemWidgetList.add(widgets);
      }
    });
  }

  Widget addFlowWidget(SystemNavDataArticles children) {
    return Container(
      margin: const EdgeInsets.only(left: 16, right: 16),
      // padding: EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 16),
      child: TextButton(
          style: ButtonStyle(
            //字体样式
            textStyle: MaterialStateProperty.all(const TextStyle(fontSize: 14)),
            //按下文字变色
            foregroundColor: MaterialStateProperty.resolveWith((states) {
              if (states.contains(MaterialState.pressed)) {
                // 按下按钮时的前景色
                return const Color(0XFF999999);
              }
              return const Color(0XFF666666);
            }),
            backgroundColor: MaterialStateProperty.resolveWith((states) {
              if (states.contains(MaterialState.pressed)) {
                return const Color(0xFFEEEEEE);
              }
              return const Color(0xFFEEEEEE);
            }),
            side: MaterialStateProperty.all(
                const BorderSide(color: Color(0x00EEEEEE))),
            minimumSize:
                MaterialStateProperty.all(const Size(double.minPositive, 24)),
            shape: MaterialStateProperty.all(const StadiumBorder(
                side: BorderSide(
              color: Color(0xFFEEEEEE),
            ))),
          ),
          onPressed: () {
            Navigator.pushNamed(context, ContentWidget.RouteName,
                arguments: ContentModel(children.link));
          },
          child: Text(children.title)),
    );
  }

  @override
  void initState() {
    getSystemList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ListView.builder(
      itemBuilder: (context, index) {
        return Column(
          children: [
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(16),
              child: Text(
                dataList[index].name,
                style: const TextStyle(fontSize: 18),
              ),
            ),
            SizedBox(
              width: double.infinity,
              child: Wrap(
                children: itemWidgetList[index],
              ),
            )
          ],
        );
      },
      itemCount: dataList.length,
    );
  }

  @override
  bool get wantKeepAlive => true;
}
